package com.erclab.internal.xpresso.dataAccess;

import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;
import com.erclab.internal.xpresso.reports.ReportView;
import com.erclab.internal.xpresso.conf.*;
import com.erclab.internal.xpresso.exceptions.*;
import com.erclab.internal.xpresso.handlers.*;

import java.sql.*;
import java.util.*;

import javax.sql.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;

/**
 * @author rodrigen
 *
 */
public class MysqlReportDAO implements ReportDAO {

	protected final String REPORT_QUERY = "SELECT * FROM reports WHERE reportName=?";
	protected final String REPORT_ELEMENTS_QUERY = "SELECT * FROM reportelements WHERE idReport=? ORDER BY DisplayOrder";
	protected final String REPORT_FILTERS_QUERY = "SELECT * FROM reportfilters WHERE idReport=? ORDER BY DisplayOrder";
	protected final String REPORT_VIEWS_QUERY = "SELECT * FROM reportviews WHERE idReport=?";
	

	public Report createReport(String reportName) throws ReportException {
		if (reportName == null)
			throw new ReportException("reportName cannot be null");
		
		Report theReport = null;    
		QueryRunner run = new QueryRunner((DataSource)Settings.getRepository().getRepositoryDataSource(reportName));
		ResultSetHandler rsHandler = new BeanHandler(Report.class);
    	
		try {
			theReport = (Report)run.query(REPORT_QUERY, reportName, rsHandler);
		}catch(SQLException ex){
			// TODO logForj
			throw new ReportException("There was a problem loading the report '" + reportName + "':" +  ex);
		}
		if (theReport == null)
			throw new ReportException("There was a problem loading the report '" + reportName + "' or it does not exist");
		
		return theReport;
	}

	public void loadReportElements(Report theReport) throws ReportException {
		// Load the report elements
		String idReport = "" + theReport.getIdReport();
		ResultSetHandler rsHandler = new ReportElementHandler();
		QueryRunner run = new QueryRunner((DataSource)Settings.getRepository().getRepositoryDataSource(theReport.getReportName()));
		try {
			theReport.setReportElements((ArrayList)run.query(REPORT_ELEMENTS_QUERY, idReport, rsHandler));
		}catch(SQLException ex){
//			TODO logForj
			throw new ReportException("There was a problem loading the report elements: " +  ex);
		}	
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.dataAccess.ReportDAO#loadReportFilters(com.erclab.internal.xpresso.reports.Report)
	 */
	public void loadReportFilters(Report theReport) throws ReportException {
		String idReport = "" + theReport.getIdReport();
		ResultSetHandler rsHandler = new ReportFilterHandler();
		QueryRunner run = new QueryRunner((DataSource)Settings.getRepository().getRepositoryDataSource(theReport.getReportName()));
		try {
			theReport.setReportFilters((ArrayList<ReportFilter>)run.query(REPORT_FILTERS_QUERY, idReport, rsHandler));
		}catch(SQLException ex){
			throw new ReportException("There was a problem loading the report elements: " +  ex);
		}	
	}
	
	public void loadReportViews(Report theReport) throws ReportException {
		String idReport = "" + theReport.getIdReport();
		ResultSetHandler rsHandler = new ReportViewHandler();
		QueryRunner run = new QueryRunner((DataSource)Settings.getRepository().getRepositoryDataSource(theReport.getReportName()));
		try {
			theReport.setReportViews((ArrayList<ReportView>)run.query(REPORT_VIEWS_QUERY, idReport, rsHandler));
		}catch(SQLException ex){
			throw new ReportException("There was a problem loading the report views: " +  ex);
		}	
		
	}

}
